//
//  XMBaseView.m
//  XinLianWang
//
//  Created by 罗晓明 on 2017/11/13.
//  Copyright © 2017年 Rowling. All rights reserved.
//

#import "XMBaseView.h"
#import "UIView+SGExtension.h"

/*******************************************UI相关宏***************************************************/
/**
 * 屏幕宽高
 */
#define SG_SCREEN_BOUNDS     ([UIScreen mainScreen].bounds)
#define SG_SCREEN_WIDTH      ([UIScreen mainScreen].bounds.size.width)
#define SG_SCREEN_HEIGHT     ([UIScreen mainScreen].bounds.size.height)
/**
 * StatusBar的高
 */
#define SG_STATUS_HEIGHT     (CGRectGetMaxY([UIApplication sharedApplication].statusBarFrame))
/**
*  适配距离
*/
#define rpx(a)               (a / 375.0 * [UIScreen mainScreen].bounds.size.width)
/**
 * 导航栏的高度
 */
#define SG_NAV_HEIGHT        44
/**
 * 导航栏+状态栏的高度
 */
#define SG_NAVSTA_HEIGHT     (SG_STATUS_HEIGHT + 44)
/**
 * 屏幕下方预留出来的高度  当设备为iphoneX时 该值为34，其他情况下均为0
 * 注：主要是针对iPhoneX，在距离iPhoneX底部布局一些UI控件时，为使得操作不被iPhoneX的横条home键遮挡需要空出来的间隙
 */
#define SG_BOTTOM_SPACE     (SG_IS_IPhoneX_All ? 34.0 : 0.0)

/*******************************************判断相关宏***************************************************/
/**
 * 判断设备
 * 该判断主要是用于做屏幕适配用，只能判断所属屏幕的规格，并不能精确的判断所属机型。
 */
#define SG_IS_IPHONE_4       ((double)[[UIScreen mainScreen] bounds].size.height == (double )480)
#define SG_IS_IPHONE_5       ((double)[[UIScreen mainScreen] bounds].size.height == (double )568)
#define SG_IS_IPHONE_6       ((double)[[UIScreen mainScreen] bounds].size.height == (double )667)
#define SG_IS_IPHONE_6_PLUS  ((double)[[UIScreen mainScreen] bounds].size.height == (double )736)
#define SG_IS_IPHONE_X       ((double)[[UIScreen mainScreen] bounds].size.height == (double )812)
#define SG_IS_IPhoneX_All    ([UIScreen mainScreen].bounds.size.height == 812 || [UIScreen mainScreen].bounds.size.height == 896)

@interface XMBaseView()
@property(nonatomic,strong)UIView * viewForXBottom;
@property(nonatomic,assign)CGFloat  buzhuBottomSpace;
@property(nonatomic,assign)BOOL  isAnmation;

@end
@implementation XMBaseView

+(instancetype)fastLogin {
    XMBaseView * view = (XMBaseView *)[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
//    view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    view.needAutoBottomView = YES;
    return view;
}

-(void)awakeFromNib {
    [super awakeFromNib];
    self.needAutoBottomView = YES;
    //添加语言通知监听
//    @weakify(self);
//    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:LanguageChange object:nil] subscribeNext:^(NSNotification *notification) {
//        @strongify(self);
//        [self languageShouldChangeUI];
//    }];
    self.buzhuBottomSpace = 10;
}

-(void)dealloc {
    NSLog(@"[%@---dealloc]",NSStringFromClass([self class]));
}

#pragma -mark:移除视图
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self touchOn666tagViewwithTouchs:touches];
}

-(void)touchOn666tagViewwithTouchs:(NSSet<UITouch *> *)touches {
    UIView * view = [self getTagView];
    if (!view) {
        return;
    }
    UITouch * touch = [touches anyObject];
    CGPoint point = [touch locationInView:view];
    if (![view.layer containsPoint:point] && self.close666TouchOutSideRemove == NO) {
        [self touchOutRemoveFromSuperView];
    }
}

-(void)touchOutRemoveFromSuperView {
    [self removeFromSuperview];
}

-(void)willMoveToSuperview:(UIView *)newSuperview {
    if (newSuperview) {
        [super willMoveToSuperview:newSuperview];
        if (!self.closeAnimation) {
            [self doAnimationShowWithView:[self getTagView]];
        }
    } else {

    }
}

-(void)removeFromSuperview {
    // 移除
    if (self.closeAnimation) {
        [super removeFromSuperview];
    } else {
        [self doAnimationCloseWithView:[self getTagView] animationcomplete:^(BOOL finished) {
            [super removeFromSuperview];
        }];
    }
}


#pragma -mark:关闭动画
-(void)doAnimationCloseWithView:(UIView *)tagView animationcomplete:(void(^)(BOOL finished))complete {
    if (tagView) {
        if (self.isAnmation) {
            return;
        }
        if (self.animationMode == FromLeft) {
           [self animationShowFromLeft:tagView isShow:NO animationcomplete:complete];
           return;
        }
        if (self.animationMode == FromRight) {
           [self animationShowFromRight:tagView isShow:NO animationcomplete:complete];
           return;
        }
        
        self.isAnmation = YES;
        CGFloat maxY = CGRectGetMaxY(tagView.frame);
        if (self.sgHeight - maxY < 100) {
            UIColor * beforeColor = self.backgroundColor;
            
            CGFloat beforey = tagView.sgY;
            CGFloat bottomViewBeforey = 0;
            if (self.viewForXBottom) {
                bottomViewBeforey = self.viewForXBottom.sgY;
            }
            [UIView animateWithDuration:0.25 animations:^{
                tagView.sgY = [UIScreen mainScreen].bounds.size.height;
                [tagView layoutIfNeeded];
                self.backgroundColor = [UIColor clearColor];
                if (self.viewForXBottom) {
                   self.viewForXBottom.sgY = [UIScreen mainScreen].bounds.size.height + tagView.sgHeight - self.buzhuBottomSpace;
                }
            } completion:^(BOOL finished) {
                if (finished) {
                    self.isAnmation = NO;
                    self.backgroundColor = beforeColor;
                    tagView.sgY = beforey;
                    if (self.viewForXBottom) {
                        self.viewForXBottom.sgY = bottomViewBeforey;
                    }
                    complete(finished);
                }
            }];
        } else {
            UIColor * beforeColor = self.backgroundColor;
            [UIView animateWithDuration:0.25 animations:^{
                tagView.alpha = 0;
                self.backgroundColor = [UIColor clearColor];
            } completion:^(BOOL finished) {
                if (finished) {
                    tagView.alpha = 1;
                    self.backgroundColor = beforeColor;
                    self.isAnmation = NO;
                    complete(finished);
                }
            }];
        }
    } else {
        complete(YES);
    }
}

#pragma -mark:出现动画
-(void)doAnimationShowWithView:(UIView *)tagView {
    [self layoutIfNeeded];
    if (tagView) {
        if (self.animationMode == FromLeft) {
           [self animationShowFromLeft:tagView isShow:YES animationcomplete:nil];
           return;
        }
        if (self.animationMode == FromRight) {
           [self animationShowFromRight:tagView isShow:YES animationcomplete:nil];
           return;
        }
        CGFloat maxY = CGRectGetMaxY(tagView.frame);
        CGFloat minY = tagView.sgY;
        if (self.sgHeight - maxY < 100) {
            CGFloat y = tagView.sgY;
            tagView.sgY = [UIScreen mainScreen].bounds.size.height;
            if (SG_IS_IPhoneX_All && self.viewForXBottom == nil && self.needAutoBottomView) {
                self.viewForXBottom = [UIView new];
                self.viewForXBottom.frame = CGRectMake(0, [UIScreen mainScreen].bounds.size.height + tagView.sgSize.height - self.buzhuBottomSpace, tagView.sgSize.width, SG_BOTTOM_SPACE + self.buzhuBottomSpace);
                [self insertSubview:self.viewForXBottom belowSubview:tagView];
                self.viewForXBottom.backgroundColor = tagView.backgroundColor;
            }
            [tagView layoutIfNeeded];
            [UIView animateWithDuration:0.25 animations:^{
                tagView.sgY = y;
                if (self.viewForXBottom) {
                    self.viewForXBottom.sgY = [UIScreen mainScreen].bounds.size.height - self.viewForXBottom.sgHeight;
                }
                [tagView layoutIfNeeded];
            }];
        } if(minY < 100) {
            self.alpha = 0;
            [UIView animateWithDuration:0.25 animations:^{
                self.alpha = 1;
            }];
        } else {
            tagView.transform = CGAffineTransformMakeScale(0.6, 0.6);
            [UIView animateWithDuration:0.3 delay:0.0 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionTransitionNone animations:^{
                tagView.transform = CGAffineTransformIdentity;
            } completion:nil];
        }
    }
}

/// 从左边出现
/// @param isShow yes 出现 no 消失
-(void)animationShowFromLeft:(UIView *)tagView isShow:(BOOL)isShow animationcomplete:(void(^)(BOOL finished))complete{
    CGFloat maxX = 0;
    CGFloat minX = -tagView.sgWidth;
    tagView.sgX = isShow ? minX : maxX;
    CGFloat toX = isShow ? maxX : minX;
    [UIView animateWithDuration:0.25 animations:^{
        tagView.sgX = toX;
        [tagView layoutIfNeeded];
    }completion:^(BOOL finished) {
        if (complete) {
            complete(finished);
        }
    }];
}

/// 从右边出现
/// @param isShow yes 出现 no 消失
-(void)animationShowFromRight:(UIView *)tagView isShow:(BOOL)isShow animationcomplete:(void(^)(BOOL finished))complete{
    CGFloat maxX = self.sgSize.width - tagView.sgWidth;
    CGFloat minX = self.sgSize.width;
    tagView.sgX = isShow ? minX : maxX;
    CGFloat toX = isShow ? maxX : minX;
    [UIView animateWithDuration:0.25 animations:^{
        tagView.sgX = toX;
        [tagView layoutIfNeeded];
    }completion:^(BOOL finished) {
        if (complete) {
            complete(finished);
        }
    }];
}

-(UIView *)getTagView {
    return [self viewWithTag:666];
}

@end


